package com.zl.gmadmin.vo.dept;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zl.gmadmin.entity.SysDept;
import com.zl.gmadmin.utils.Builder;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2021/2/26 13:52
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class DeptVo implements Serializable {
    private Long id;

    private String name;

    private Boolean enabled;

    private Integer deptSort;

    private List<DeptVo> children;

    private Long pid;

    private Integer subCount;

        /**
     * 创建者
     */
    private String createBy;

    /**
     * 更新者
     */
    private String updateBy;

    /**
     * 创建日期
     */
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    private Boolean hasChildren;

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public void setDeptSort(Integer deptSort) {
        this.deptSort = deptSort;
    }

    public void setChildren(List<DeptVo> children) {
        this.children = children;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public void setSubCount(Integer subCount) {
        this.subCount = subCount;
    }

    public Boolean getHasChildren() {
        return subCount > 0;
    }

    /**
     * 是子节点
     * @return
     */
    public Boolean getIsLeaf() {
        return subCount <= 0;
    }

    public String getLabel() {
        return name;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public Long getPid() {
        return pid;
    }

    public List<DeptVo> getChildren() {
        return children;
    }

    public Integer getSubCount() {
        return subCount;
    }

    public String getName() {
        return name;
    }

    public Integer getDeptSort() {
        return deptSort;
    }

    public void setHasChildren(Boolean hasChildren) {
        this.hasChildren = hasChildren;
    }

    public static <R> DeptVo build(SysDept dept) {
        DeptVo deptVo= Builder.of(DeptVo::new)
                .with(DeptVo::setPid,dept.getPid())
                .with(DeptVo::setId,dept.getId())
                .with(DeptVo::setEnabled,dept.getEnabled())
                .with(DeptVo::setName,dept.getName())
                .with(DeptVo::setSubCount,dept.getSubCount())
                .with(DeptVo::setDeptSort,dept.getDeptSort())
                .with(DeptVo::setHasChildren,dept.getSubCount()>0)
                .with(DeptVo::setCreateBy, dept.getCreateBy())
                .with(DeptVo::setCreateTime, dept.getCreateTime())
                .with(DeptVo::setUpdateBy, dept.getUpdateBy())
                .with(DeptVo::setUpdateTime, dept.getUpdateTime())
                .build();


        return deptVo;

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {return true;}
        if (o == null || getClass() != o.getClass()) {return false;}
        DeptVo deptVo = (DeptVo) o;
        return Objects.equals(id, deptVo.id) &&
                Objects.equals(name, deptVo.name) &&
                Objects.equals(enabled, deptVo.enabled) &&
                Objects.equals(deptSort, deptVo.deptSort) &&
                Objects.equals(children, deptVo.children) &&
                Objects.equals(pid, deptVo.pid) &&
                Objects.equals(subCount, deptVo.subCount);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, enabled, deptSort, children, pid, subCount);
    }
}
